
const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();
const { module: moduleName } = app.siteInfo;
const util = app.util;
const utils = require('../../../ws_micro_shop/utils/utils.js');
const { qyWXCheckSeesion } = require('../../../common/utils/ws/ws-wxwork');

Page({
  data: {
    type: 1,
    _switch: 0,
    page: 1,
    keyword: '',
    isEmpty: true,
    curr: -1,
    sort: 0,
    sort_type: -1,
    isTotalShow: false,
    isHidden: false,
    // isScrollShow: false,
    // status_type: 0,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-15.png',
      text: '该页面暂无商品',
    },
    height: wx.systemInfo.windowHeight,
    cat_style: [],
    isShow: false,
    list: [],
    recentList: [
      {
        name: '不限',
        id: 0,
      },
      {
        name: '新品',
        id: 1,
      },
      {
        name: '热销',
        id: 2,
      },
      {
        name: '限时折扣',
        id: 3,
      },
      {
        name: '秒杀',
        id: 4,
      },
      {
        name: '拼团',
        id: 5,
      },
    ],
    data: {},
    cat_id: 0,
    currentnum: 0,
    isData: false,
    loadingText: '下拉加载',
    imgUrl: app.imgUrl,
    canPullUp: true,
    share_modal_active: '',
    imgUrl: app.imgUrl
  },
  onLoad(opts) {
    wx.hideShareMenu();
    let that = this;
    let options = opts || {};
    that.setData({
      opts,
      currentnum: options.num || '',
      goods_id: options.id || '',
      isShow: true,
      isData: false,
      list: [],
      stroeId: options.shopid,
      rolesId: options.guide_id,
      isSaas: app.ws_micro_shop_globalData.isSaas,
    }, () => {
      app.ws_micro_shoplogin().then(res => {
        app.isWXWork && qyWXCheckSeesion({});
        that.loadStart();
      })
    });
  },
  loadStart() {
    let _this = this;
    let { goods_id, cat_id, sort, sort_type, data, status_type, currentnum, stroeId: shop_id = '' } = _this.data;
    data.shop_id = shop_id;
    data.page = 1;
    data.is_integral = 0;
    data.sort = sort;
    data.sort_type = sort_type;
    cat_id && (data.cat_id = cat_id);
    data.currentnum = currentnum;
    if (data.currentnum && data.currentnum > 3) {
      data.cat_id = 0;
      _this.setData({
        curr: -1,
        // toView: 'item-1',
      });
    }
    status_type && (data.status_type = status_type);
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.guide.goods_list,
      data,
      success: function (res) {

        if (res.code === 0 && res.data.list && res.data.list.length > 0) {
          _this.setData({
            list: res.data.list,
            goods: res.data.list.find(function(elem){
              return elem.id == goods_id;
            })
          });
          console.info('-----------goods-----------', _this.data.goods);

        } else {
          _this.setData({
            list: [],
          });
        }
      },
      complete: res => {
        this.setData({
          isData: true,
        }, () => {
          utils.after(res.data, this);
          wx.hideLoading();
        });
      },
    });
  },
  onReachBottom() {
    let { page, canPullUp } = this.data;
    if (canPullUp) {
      this.setData({
        page: ++page,
      }, () => {
        this.loadmore();
      });
    }
  },
  loadmore() {
    let { list, page, cat_id, sort, sort_type, data, status_type, stroeId: shop_id = '' } = this.data;

    data.shop_id = shop_id;
    data.page = page;
    data.is_integral = 0;
    data.sort = sort;
    data.sort_type = sort_type;
    cat_id && (data.cat_id = cat_id);
    if (data.currentnum && data.currentnum > 3) {
      data.cat_id = 0;
      this.setData({
        curr: -1,
        // toView: 'item-1',
      });
    }
    status_type && (data.status_type = status_type);
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });

    app.ws_micro_shoprequest({
      url: api.guide.goods_list,
      data,
      success: res => {
        if (res && res.code === 0) {
          const newList = res.data.list;
          if (newList.length <= 0) {
            this.setData({
              canPullUp: false,
              loadingText: '—— 到底啦 ——',
            });
            return;
          }
          this.setData({
            list: [...list, ...newList],
          }, () => {
            utils.after(res, this);
          });
        }
      },
      fail: res => {
        utils.after(res, this);
      },
      complete: res => {
        wx.hideLoading();
      },
    });
  },
  onUserOpStatistic: function (e) {
    // console.info('----------onUserOpStatistic--------', e);
    //if (e.op == 'share') {
      // let { goods, currentnum, goods_id } = this.data;
      // let share_type = currentnum > 3 ? Number(currentnum) : Number(goods.label_tag);
      // app.handleStatisticsShare(share_type, goods_id);
    //}
  },
  onShareAppMessage(e) {
    // if (e.from === 'button') {
      let _this = this;
      let { goods_id, goods, stroeId = '', rolesId = '', currentnum } = _this.data;
      let share_type = currentnum > 3 ? Number(currentnum) : Number(goods.label_tag);
      let url = share_type === 5 ? '/ws_micro_shop_pt/pages/pt/details/details?gid=' : (share_type === 4 ? '/ws_micro_shop_ms/pages/miaosha/details/details?id=' : '/ws_micro_shop/pages/goods/goods?id=');
      let path = url + goods_id + '&guide_id=' + rolesId + '&shopid=' + stroeId;
      path = util.spliceSharePath(path, '4', moduleName);
      let title = goods.name;
      let imageUrl = goods.pic_url || goods.cover_pic;
      console.info('-------path-----------', path);
      return {
        path,
        title: title,
        imageUrl: imageUrl,
      };
    // }
  },
  onNavigate() {
    let { goods_id, stroeId = '', goods, currentnum } = this.data;
      let share_type = currentnum > 3 ? Number(currentnum) : Number(goods.label_tag);
    let url = share_type === 5 ? '/ws_micro_shop_pt/pages/pt/details/details' : (share_type === 4 ? '/ws_micro_shop_ms/pages/miaosha/details/details' : '/ws_micro_shop/pages/goods/goods');
    let params = {
      shopid: stroeId
    }
    share_type === 5 ? params.gid = goods_id : params.id = goods_id;
    app.util.navigateTo(url, params);
  },

  selectgoods(e) {
    let dataset = e.currentTarget.dataset;
    this.setData(
      dataset
    );
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0, // android机子滑动会从上往下，动画时长为0可以暂时解决，不过体验不太好
    });
  }
});
